package com.yizisu.basemvvm.view

import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.LayoutRes
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.yizisu.basemvvm.utils.dip

/**
 * 创建一个简单的文本recyclerView适配器
 */
fun simpleTextRcvAdater(
    rcv: RecyclerView? = null/*recyclerView*/,
    itemDatas: MutableList<String>? = null/*数据集合*/
): SimpleRcvAdapter<String> {
    val adapter = simpleRcvAdapter(
        rcv,
        0,
        itemDatas
    ) { holder: SimpleRcvAdapter.SimpleRcvHolder, position: Int, itemData: String ->
        (holder.itemView as TextView).text = itemData
    }
    adapter.setOnCreateHolderViewListener { inflater, parent, viewType ->
        return@setOnCreateHolderViewListener TextView(parent.context).apply {
            setPadding(dip(16), 0, dip(16), 0)
            gravity = Gravity.CENTER_VERTICAL
            setTextColor(Color.BLACK)
            layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, dip(40))
        }
    }
    return adapter
}

fun <ItemData> simpleRcvAdapter(
    rcv: RecyclerView?,
    @LayoutRes itemLayoutRes: Int/*布局文件，如果使用其他方式创建itemView，则这里设置无效*/,
    itemDatas: MutableList<ItemData>?/*数据集合*/,
    onBindItemView: (holder: SimpleRcvAdapter.SimpleRcvHolder, position: Int, itemData: ItemData) -> Unit/*绑定数据监听*/
): SimpleRcvAdapter<ItemData> {
    if (rcv?.layoutManager == null) {
        rcv?.layoutManager = LinearLayoutManager(rcv?.context)
    }
    rcv?.addItemDecoration(DividerItemDecoration(rcv.context, DividerItemDecoration.VERTICAL))
    val adapter = SimpleRcvAdapter(
        itemLayoutRes,
        itemDatas,
        onBindItemView
    )
    rcv?.adapter = adapter
    return adapter
}

/**
 * 提供一个直接用new方式创建recyclerView适配器
 */
class SimpleRcvAdapter<ItemData>(
    @LayoutRes private val itemLayoutRes: Int/*布局文件，如果使用其他方式创建itemView，则这里设置无效*/,
    itemDatas: MutableList<ItemData>?/*数据集合*/,
    private val onBindItemView: (holder: SimpleRcvHolder, position: Int, itemData: ItemData) -> Unit/*绑定数据监听*/
) : BaseRcvAdapter<ItemData, SimpleRcvAdapter.SimpleRcvHolder>(itemDatas) {

    private var onGetItemView: Function3<LayoutInflater, ViewGroup, Int, View?>? = null
    private var onItemViewCreate: Function1<View, Unit>? = null

    /**
     * 创建ItemView，可以返回null
     */
    fun setOnCreateHolderViewListener(l: (inflater: LayoutInflater, parent: ViewGroup, viewType: Int) -> View?) {
        onGetItemView = l
    }

    /**
     * 设置view创建完毕监听,不管何种方法创建holderView，创建完毕均会走这个方法
     */
    fun setOnHolderViewCreateListener(l: (view: View) -> Unit) {
        onItemViewCreate = l
    }

    override fun getItemLayoutView(
        inflater: LayoutInflater,
        parent: ViewGroup,
        viewType: Int
    ): View? {
        return onGetItemView?.invoke(inflater, parent, viewType)
    }

    override fun getItemLayoutRes(): Int = itemLayoutRes

    override fun onCreateViewHolder(itemView: View): SimpleRcvHolder {
        onItemViewCreate?.invoke(itemView)
        return SimpleRcvHolder(itemView)
    }

    override fun onBindViewHolder(holder: SimpleRcvHolder, position: Int, itemData: ItemData) {
        onBindItemView.invoke(holder, position, itemData)
    }

    class SimpleRcvHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        /**
         * 可以保存一些数据，比如view集合
         */
        var tag: Any? = null
    }
}